package club.kunlingou.dsc.sqlclient.controller;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import club.kunlingou.dsc.sqlclient.service.SqlClientService;

@Controller
@RequestMapping("sqlclient")
public class SqlClientController {
	
	@Autowired
	SqlClientService clientService;
	
	@RequestMapping("index")
	@ResponseBody
	public JSONObject index() {
		JSONObject jsonObject = new JSONObject();
		Date date = new Date();
		jsonObject.put("index", date);
		return jsonObject;
	} 
	
	@RequestMapping("query")
	@ResponseBody
	public JSONObject query(@RequestBody JSONObject data) throws SQLException {
		String sql = data.getString("sql");
		JSONObject jsonObject = new JSONObject();
		
//		String sql = "select * from engine_demo_table";
		
		JSONObject result = clientService.getResult(sql);
		
		Date date = new Date();
		jsonObject.put("code", 0);
		jsonObject.put("date", date);
		jsonObject.put("result", result);
		return jsonObject;
	} 
}
